//
//  ViewController.m
//  you
//
//  Created by seven on 16/5/16.
//  Copyright © 2016年 seven. All rights reserved.
//

#import "ViewController.h"
#import "XXPlayView.h"
typedef CGPoint (^pointAddHandler)(CGPoint * point);

@interface ViewController ()
@property(nonatomic,weak) XXPlayView  *playView ;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"XXPlayView";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
    }
    
    
//    XXPlayView *playView = [[XXPlayView alloc]init];
//    playView.videoUrl = [NSURL URLWithString:@"http://baobab.wdjcdn.com/14577085072690.mp4"];
//    playView.titleStr = @"这是一个视频" ;
//    playView.frame = CGRectMake(0, 0, XXScreenWidth,[XXPlayView heightRatio]);
//    
//    [cell addSubview:playView];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.playView.superview) {
        [self.playView removeFromSuperview];

    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     XXPlayView *playView =  [[XXPlayView alloc]init];
     playView.videoUrl = [NSURL URLWithString:@"http://baobab.wdjcdn.com/14577085072690.mp4"];
     playView.titleStr = [NSString stringWithFormat:@"这是一个视频%@",@(indexPath.row)];
     playView.frame = cell.bounds ;
    [playView clickPlayButton];
     NSLog(@"playView.frame = %@ ",NSStringFromCGRect(self.playView.frame));
    [cell addSubview:playView];
    self.playView = playView ;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y =  scrollView.contentOffset.y ;
    
    CGRect playViewFrameInTableView = [self.playView convertRect:self.playView.frame toView:scrollView  ];
    CGFloat playViewMaxHeightInTableView =  CGRectGetMaxY(playViewFrameInTableView) ;
    
    NSLog(@"y=%@ ,playViewMaxHeightInTableView = %@ ", @(y) , @(playViewMaxHeightInTableView ) );

    if ( y > playViewMaxHeightInTableView ) {
        if (self.playView.superview) {
            [self.playView removeFromSuperview];
        }
        NSLog(@" XXPlayView 消失了 ");
    }
}
//- (XXPlayView *)playView {
//    if (!_playView) {
//        _playView = [[XXPlayView alloc]init];
//    }
//    return _playView ;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return [XXPlayView heightRatio] + 2 ;
}
@end
